package com.zhifu.union;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.App;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.help.UserHelper;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.Index;
import com.mangowang.lib.common.util.MMKVUtil;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.module.login.ui.LoginActivity;
import com.mangowang.module.main.ui.MainActivity;

public class StartActivity extends BaseActivity {
    private String IS_FIRST = "is_have_welcome";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTranslucentStatus(this);
        boolean isFirst = MMKVUtil.getBoolean(IS_FIRST);
        NetworkManager.INSTANCE.post(Apis.get_site_domain, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                App.setServiceHost(Index.get_site_domain.parseFrom(response).getHost());
                MMKVUtil.setString(NetworkManager.INSTANCE.SERVER_HEAD,Index.get_site_domain.parseFrom(response).getHost());
                new Handler().postDelayed(() -> {
                    if (UserHelper.isLogin() && UserHelper.getIsRenZhen() == 1 && UserHelper.getIsBank() == 1) {
                        Intent intent = new Intent(StartActivity.this, MainActivity.class);
                        if (getIntent().getStringExtra("EXTRA_BUNDLE") != null) {
                            intent.putExtra("EXTRA_BUNDLE",
                                    getIntent().getStringArrayExtra("EXTRA_BUNDLE"));
                        }
                        startActivity(intent);
                    } else {
                        UserHelper.loginOut();
                        startActivity(LoginActivity.class);
                    }
                    finish();
                }, 1000);
            }

            @Override
            public void onError(int code, String errorMessage) {
                showHintDialog(errorMessage);
            }
        });
    }
}